package com.example.sticky_notes;

import java.util.ArrayList;
import java.util.List;

import android.app.ActionBar;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.example.finalapp.R;
import com.example.finalapp.SplitActionBarActivity;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;

public class StickyNoteActivity extends SplitActionBarActivity {
	ListView lv;
	CustomAdapter adapter;
	STnotesArray stickyArr = STnotesArray.getInstance();
	boolean isServerDone;
	ProgressDialog ringProgressDialog;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sticky_note);

		ActionBar actionBar = getActionBar();
		actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

		actionBar.setCustomView(R.layout.action_sticky_main);
		final ImageButton addButtonCustom = (ImageButton)findViewById(R.id.sticky_addBtn);
		
		isServerDone = false;

		//build stickyNote array from DB parse
		ParseQuery<ParseObject> query = ParseQuery.getQuery("StickyNotes");
		query.whereEqualTo("Apartment", ParseUser.getCurrentUser().getString("Apartment"));
		query.findInBackground(new FindCallback<ParseObject>() {
			public void done(List<ParseObject> notes, ParseException e) 
			{
				if (e == null) 
				{
					for(ParseObject note : notes)
					{//for each note from notes
						StickyNoteObj new_note = new StickyNoteObj();
						new_note.headLine = note.getString("HeadLine");
						new_note.note = note.getString("Note");
						new_note.owner = note.getString("Owner");
						new_note.updatedDate = note.getDate("updatedAt");
						new_note.id = note.getString("objectId");
						StickyNoteActivity.this.stickyArr.add(new_note);
					}
				} 
				else
				{
					//error
				}
//				StickyNoteActivity.this.isServerDone = true; 
			}
		});

	//	ringProgressDialog.show();
//		ringProgressDialog = ProgressDialog.show(StickyNoteActivity.this, "Please wait ...", "Downloading from server ...", true);
//		ringProgressDialog.setCancelable(false);
//		while(!isServerDone){} //wait to server to get done in background
//		ringProgressDialog.dismiss();		

		lv= (ListView)findViewById(R.id.list_sticky);
		adapter= new CustomAdapter(this, R.id.sticky_textview, stickyArr);
		lv.setAdapter(adapter);

		//set listeners for buttons
		addButtonCustom.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) 
			{
				// Switching to Edit New note screen
				Intent intent = new Intent(StickyNoteActivity.this, Edit_new_noteActivity.class);
				startActivity(intent);
			}
		});

		//onitemclick for listview
		lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// Switching to Edit note screen
				Intent intent = new Intent(StickyNoteActivity.this, Edit_new_noteActivity.class);
				
				intent.putExtra(Edit_new_noteActivity.HEAD_LINE, stickyArr.get(position).headLine);
				intent.putExtra(Edit_new_noteActivity.NOTE, stickyArr.get(position).note);
				intent.putExtra(Edit_new_noteActivity.ID, stickyArr.get(position).id);
				intent.putExtra(Edit_new_noteActivity.IS_EXIST, true);
				intent.putExtra(Edit_new_noteActivity.POSITION, position);

				startActivity(intent);
			}

		});
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		adapter.notifyDataSetChanged();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.sticky_note, menu);
		return true;
	}
	
	class CustomAdapter extends ArrayAdapter<StickyNoteObj>{

		Context context; 
		int layoutResourceId;    
		ArrayList<StickyNoteObj> data = null;
		private LayoutInflater mInflater; 	

		public CustomAdapter(Context customAdapter, int layoutResourceId, ArrayList<StickyNoteObj> data) {

			super(customAdapter, layoutResourceId, data);	
			this.layoutResourceId = layoutResourceId;
			this.context = customAdapter;
			this.data = data;
			this.mInflater = LayoutInflater.from(customAdapter);						
		}

		public View getView(final int position, View convertView, ViewGroup parent) {		

			ViewHolder holder = null;		       

			if (convertView == null) {

				//item_list
				convertView = mInflater.inflate(R.layout.sticky_note_item, null);

				holder = new ViewHolder();

				//fill the views
				holder.title = (TextView) convertView.findViewById(R.id.sticky_textview);

				convertView.setTag(holder);						
			} 
			else {
				// Get the ViewHolder back to get fast access to the TextView
				holder = (ViewHolder) convertView.getTag();//			
			}
			holder.title.setText(data.get(position).headLine);

			return convertView;
		}


		class ViewHolder {		
			TextView title;	
		}

	}

}
